<template>
  <el-dialog :title="!dataForm.id ? '新增会员卡类型' : '修改会员卡类型'"
             :close-on-click-modal="false"
             :visible.sync="visible"
             width="680px">
    <!-- label-position="top" -->
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="120px"
             size="small"
             style="margin-left:10px; margin-right: 80px;">
      <el-form-item label="类型名称"
                    prop="name">
        <el-input v-model="dataForm.name"
                  placeholder="类型名称"></el-input>
      </el-form-item>
      <el-form-item label="类型代码"
                    prop="code">
        <el-input v-model="dataForm.code"
                  placeholder="类型代码"></el-input>
      </el-form-item>
      <el-form-item label="默认可用次数"
                    prop="defusage">
        <el-input v-model="dataForm.defusage"
                  placeholder="默认可用次数"></el-input>
      </el-form-item>
      <el-form-item label="当日刷卡次数"
                    prop="constrains">
        <el-input v-model="dataForm.constrains"
                  placeholder="当日刷卡次数,0表示不受刷卡限制"></el-input>
      </el-form-item>
      <!-- <el-form-item label="消费折扣"
                    prop="discount">
        <el-input v-model="dataForm.discount"
                  placeholder="消费折扣"></el-input>
      </el-form-item> -->
      <el-form-item label="推荐人数"
                    prop="require">
        <el-input v-model="dataForm.require"
                  placeholder="推荐人数"></el-input>
      </el-form-item>
      <el-form-item label="推荐会员类型">
        <el-input v-model="dataForm.requireCode"
                  placeholder="推荐会员类型代码"></el-input>
      </el-form-item>

      <el-form-item label="备注"
                    prop="remark">
        <el-input v-model="dataForm.remark"
                  placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        // discount: '',
        require: '',
        requireCode: '',
        code: '',
        defusage: '',
        constrains: '',
        remark: ''
      },
      dataRule: {
        name: [
          { required: true, message: '类型名称不能为空', trigger: 'blur' }
        ],
        // discount: [
        //   { required: true, message: '消费折扣不能为空', trigger: 'blur' }
        // ],
        // require: [
        //   { required: true, message: '推荐人数不能为空', trigger: 'blur' }
        // ],
        // requireCode: [
        //   { required: true, message: '推荐类型代码不能为空', trigger: 'blur' }
        // ],
        code: [
          { required: true, message: '类型代码不能为空', trigger: 'blur' }
        ],
        constrains: [
          { required: true, message: '当日限制刷卡次数不能为空', trigger: 'blur' }
        ]
        // remark: [
        //   { required: true, message: '备注不能为空', trigger: 'blur' }
        // ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/member/cardtype/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.cardtype.name
              this.dataForm.code = data.cardtype.code
              this.dataForm.defusage = data.cardtype.defusage
              // this.dataForm.discount = data.cardtype.discount
              this.dataForm.require = data.cardtype.require
              this.dataForm.requireCode = data.cardtype.requireCode
              this.dataForm.constrains = data.cardtype.constrains
              this.dataForm.remark = data.cardtype.remark
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      const reg = /^\d{1,}$/
      if (this.dataForm.defusage && !reg.test(this.dataForm.defusage)) {
        this.$alert('默认可用次数必须为整数数字', '温馨提示', {
          confirmButtonText: '确定',
          type: 'error'
        })
        return
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/member/cardtype/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'code': this.dataForm.code,
              'defusage': this.dataForm.defusage,
              // 'discount': this.dataForm.discount,
              'require': this.dataForm.require,
              'requireCode': this.dataForm.requireCode,
              'constrains': this.dataForm.constrains,
              'remark': this.dataForm.remark
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: this.dataForm.id ? '会员卡类型更新成功' : '会员卡类型新增成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
div >>> .el-dialog__body {
  padding: 10px 20px;
}
</style>